﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WuDada.Core.Post.Domain;
using WuDada.Core.Persistence.Ado;
using WuDada.Core.Persistence;
using NHibernate.Criterion;
using System.Collections;
using WuDada.Core.Generic.Extension;

namespace WuDada.Core.Post.Persistence
{
    public class TagDao : AdoDao, ITagDao
    {
        public INHibernateDao NHibernateDao { get; set; }        

        /// <summary>
        /// 新增Tag
        /// </summary>
        /// <param name="tagVO">被新增的Tag</param>
        /// <returns>新增後的Tag</returns>
        public TagVO CreateTag(TagVO tagVO)
        {
            NHibernateDao.Insert(tagVO);

            return tagVO;
        }

        /// <summary>
        /// 取得Tag By TagId
        /// </summary>
        /// <param name="tagId">TagId</param>
        /// <returns>Tag</returns>
        public TagVO GetTagById(int tagId)
        {
            return NHibernateDao.GetVOById<TagVO>(tagId);
        }        

        /// <summary>
        /// 刪除Tag
        /// </summary>
        /// <param name="tagVO">被刪除的Tag</param>
        public void DeleteTag(TagVO tagVO)
        {
            NHibernateDao.Delete(tagVO);
        }

        /// <summary>
        /// 更新Tag
        /// </summary>
        /// <param name="tagVO">被更新的Tag</param>
        /// <returns>更新後的Tag</returns>
        public TagVO UpdateTag(TagVO tagVO)
        {
            NHibernateDao.Update(tagVO);

            return tagVO;
        }

        /// <summary>
        /// 取得Tag清單
        /// </summary>
        /// <param name="conditions">搜尋條件</param>
        /// <returns>電子Tag清單</returns>
        public IList<TagVO> GetTagList(IDictionary<string, string> conditions)
        {
            ArrayList param = new ArrayList();
            string fromScript = "select t from TagVO t ";
            StringBuilder whereScript = new StringBuilder();
            return this.QueryTag(param, fromScript, whereScript, conditions, true).OfType<TagVO>().ToList();
        }

        /// <summary>
        /// 取得Tag數量
        /// </summary>
        /// <param name="conditions"></param>
        /// <returns></returns>
        public int GetTagCount(IDictionary<string, string> conditions)
        {
            int count = 0;
            ArrayList param = new ArrayList();
            string fromScript = "select count(t.TagId) from TagVO t ";
            StringBuilder whereScript = new StringBuilder();
            IList result = this.QueryTag(param, fromScript, whereScript, conditions, false);
            if (result.Count > 0)
            {
                count = Convert.ToInt32(result[0]);
            }
            return count;
        }

        private IList QueryTag(ArrayList param, string fromScript, StringBuilder whereScript, IDictionary<string, string> conditions, bool useOrder)
        {
            AppendTagTargetId(conditions, whereScript, param);
            AppendTagOnwerId(conditions, whereScript, param);
            AppendTagType(conditions, whereScript, param);

            string hql = fromScript + "where 1=1 " + whereScript;
            if (useOrder)
            {
                hql += AppendTagOrder(conditions);
            }

            return NHibernateDao.Query(hql, param, conditions);
        }

        private void AppendTagType(IDictionary<string, string> conditions, StringBuilder whereScript, ArrayList param)
        {
            if (conditions.IsContainsValue("TagType"))
            {
                whereScript.Append(" and t.TagType = ? ");
                param.Add(int.Parse(conditions["TagType"]));
            }
        }

        private void AppendTagOnwerId(IDictionary<string, string> conditions, StringBuilder whereScript, ArrayList param)
        {
            if (conditions.IsContainsValue("OnwerId"))
            {
                whereScript.Append(" and t.OnwerId = ? ");
                param.Add(conditions["OnwerId"]);
            }
        }

        private void AppendTagTargetId(IDictionary<string, string> conditions, StringBuilder whereScript, ArrayList param)
        {
            if (conditions.IsContainsValue("TargetId"))
            {
                whereScript.Append(" and t.TargetId = ? ");
                param.Add(conditions["TargetId"]);
            }
        }

        private string AppendTagOrder(IDictionary<string, string> conditions)
        {
            //// 排序條件
            string order = "order by t.CreatedDate desc ";
            if (conditions.IsContainsValue("Order"))
            {
                order = conditions["Order"];
            }

            return order;
        }
    }
}
